import 'package:cached_network_image/cached_network_image.dart';
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:www.znpigai.teacher/global_store/state.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    StudentMineState state, Dispatch dispatch, ViewService viewService) {
  bool isMember() {
    return state.userInfo != null &&
        state.userInfo.containsKey('accountInfo') &&
        (state.userInfo['accountInfo']['validRechargeUser'] ||
            state.userInfo['accountInfo']['validBillingCardUser']);
  }

  String memberString() {
    if (isMember()) {
      String time = state.userInfo['accountInfo']['rechargeExpiryTime'];
      if (time == null || time.isEmpty) {
        time = state.userInfo['accountInfo']['billingCardExpiryDate'] ?? '';
      }
      return "会员到期时间：$time";
    } else {
      return '您还不是会员哦！';
    }
  }

  return SingleChildScrollView(
    child: Column(
      children: <Widget>[
        Card(
            key: null,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10.0),
            ),
            margin: EdgeInsets.all(10),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                SizedBox(height: 10),
                FlatButton(
                  child: Stack(
                    alignment: Alignment.center, //指定未定位或部分定位widget的对齐方式
                    children: <Widget>[
                      CachedNetworkImage(
                        width: 60,
                        height: 60,
                        placeholder: (context, url) =>
                            CircularProgressIndicator(),
                        errorWidget: (context, url, error) =>
                            Image.asset("assets/images/user.png"),
                        imageUrl: GlobalState.baseUrl(
                            state != null && state.userInfo != null
                                ? state.userInfo['headImage']
                                : ''),
                      ),
                      Positioned(
                        right: 0,
                        bottom: 0,
                        width: 20,
                        height: 20,
                        child: Opacity(
                          opacity: isMember() ? 1.0 : 0.0,
                          child: Image.asset("assets/images/member.png"),
                        ),
                      ),
                    ],
                  ),
                  onPressed: () =>
                      dispatch(StudentMineActionCreator.onAvatar()),
                ),
                Text(state.userInfo != null ? state.userInfo['name'] : ''),
                Text(memberString()),
                SizedBox(height: 10),
                Row(
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: Column(
                        children: <Widget>[
                          Text(
                            '学习天数',
                            style: TextStyle(color: Colors.grey),
                          ),
                          SizedBox(height: 10),
                          Text(
                              state.userInfo != null
                                  ? state.userInfo['statistics']['learningDay']
                                      .toString()
                                      .replaceAll('null', '0')
                                  : '',
                              style: TextStyle(color: Colors.blue))
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Column(
                        children: <Widget>[
                          Text(
                            '练习次数',
                            style: TextStyle(color: Colors.grey),
                          ),
                          SizedBox(height: 10),
                          Text(
                              state.userInfo != null
                                  ? state.userInfo['statistics']
                                          ['practiceCount']
                                      .toString()
                                      .replaceAll('null', '0')
                                  : '',
                              style: TextStyle(color: Colors.blue))
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Column(
                        children: <Widget>[
                          Text(
                            '最佳成绩',
                            style: TextStyle(color: Colors.grey),
                          ),
                          SizedBox(height: 10),
                          Text(
                              state.userInfo != null
                                  ? state.userInfo['statistics']['bestScore']
                                      .toString()
                                      .replaceAll('null', '0')
                                  : '',
                              style: TextStyle(color: Colors.blue))
                        ],
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 10),
              ],
            )),
        Row(
          children: <Widget>[
            Expanded(
                flex: 1,
                child: Card(
                    key: null,
                    margin: EdgeInsets.only(left: 10, right: 5),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10.0),
                    ),
                    child: FlatButton(
                        onPressed: () =>
                            dispatch(StudentMineActionCreator.onPay()),
                        child: Padding(
                          padding: EdgeInsets.all(10),
                          child: Text('去充值>',
                              style: TextStyle(color: Colors.blue)),
                        )))),
//            Expanded(
//                flex: 1,
//                child: Card(
//                    key: null,
//                    margin: EdgeInsets.only(right: 10, left: 5),
//                    shape: RoundedRectangleBorder(
//                      borderRadius: BorderRadius.circular(10.0),
//                    ),
//                    child: FlatButton(
//                        onPressed: () =>
//                            dispatch(StudentMineActionCreator.onReport()),
//                        child: Padding(
//                          padding: EdgeInsets.all(10),
//                          child: Text('查看学情报告>',
//                              style: TextStyle(color: Colors.blue)),
//                        )))),
          ],
        ),
        Card(
            key: null,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10.0),
            ),
            margin: EdgeInsets.all(10),
            child: new Column(
              children: [
                Container(
                  height: 54.0,
                  child: ListTile(
                    title: new Text('我的消息'),
                    leading: Container(
                      width: 32.0,
                      height: 32.0,
                      padding: EdgeInsets.all(4.0),
                      child: ImageIcon(
                        AssetImage("assets/images/mine_msg.png"),
                        color: Colors.blue,
                        size: 24.0,
                      ),
                    ),
                    trailing: ImageIcon(
                      AssetImage("assets/images/right.png"),
                      size: 24.0,
                    ),
                    onTap: () => dispatch(StudentMineActionCreator.onMessage()),
                  ),
                ),
                new Divider(
                  height: 1,
                  indent: 60,
                ),
                Container(
                  height: 54.0,
                  child: ListTile(
                    title: new Text('我的班级'),
                    leading: Container(
                      width: 32.0,
                      height: 32.0,
                      padding: EdgeInsets.all(4.0),
                      child: ImageIcon(
                        AssetImage("assets/images/mine_class.png"),
                        color: Colors.blue,
                        size: 24.0,
                      ),
                    ),
                    trailing: ImageIcon(
                      AssetImage("assets/images/right.png"),
                      size: 24.0,
                    ),
                    onTap: () => dispatch(StudentMineActionCreator.onClass()),
                  ),
                ),
                new Divider(
                  height: 1,
                  indent: 60,
                ),
                Container(
                  height: 54.0,
                  child: ListTile(
                    title: new Text('设置'),
                    leading: Container(
                      width: 32.0,
                      height: 32.0,
                      padding: EdgeInsets.all(4.0),
                      child: ImageIcon(
                        AssetImage("assets/images/mine_setting.png"),
                        color: Colors.blue,
                        size: 24.0,
                      ),
                    ),
                    trailing: ImageIcon(
                      AssetImage("assets/images/right.png"),
                      size: 24.0,
                    ),
                    onTap: () => dispatch(StudentMineActionCreator.onProfile()),
                  ),
                ),
                new Divider(
                  height: 1,
                  indent: 60,
                ),
                Container(
                  height: 54.0,
                  child: ListTile(
                    title: Text('意见反馈'),
                    leading: Container(
                      width: 32.0,
                      height: 32.0,
                      padding: EdgeInsets.all(4.0),
                      child: ImageIcon(
                        AssetImage("assets/images/mine_feedback.png"),
                        color: Colors.blue,
                        size: 24.0,
                      ),
                    ),
                    trailing: ImageIcon(
                      AssetImage("assets/images/right.png"),
                      size: 24.0,
                    ),
                    onTap: () =>
                        dispatch(StudentMineActionCreator.onFeedback()),
                  ),
                ),
                new Divider(
                  height: 1,
                  indent: 60,
                ),
                Container(
                  height: 54.0,
                  child: ListTile(
                    title: Text('关于'),
                    leading: Container(
                      width: 32.0,
                      height: 32.0,
                      padding: EdgeInsets.all(4.0),
                      child: ImageIcon(
                        AssetImage("assets/images/mine_about.png"),
                        color: Colors.blue,
                        size: 24.0,
                      ),
                    ),
                    trailing: ImageIcon(
                      AssetImage("assets/images/right.png"),
                      size: 24.0,
                    ),
                    onTap: () => dispatch(StudentMineActionCreator.onAbout()),
                  ),
                ),
                SizedBox(height: 10),
              ],
            )),
      ],
    ),
  );
}
